using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;
using Rocuna.Controls;
using Rocuna.GameEngine.Extension.WP7.EventArgs;
using Rocuna.GameEngine.Extension.WP7.Input.Constants;
using Rocuna.GameEngine.Graphics;
using Rocuna.GameEngine.Graphics.Cameras;
using Rocuna.GameEngine.Input;

namespace Rocuna.GameEngine.Extension.WP7.Controls
{
    /// <summary>
    /// Special Joystick control for phone devices.
    /// </summary>
    public class Joystick : JoystickBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Control"/> class.
        /// </summary>
        /// <param name="game">The Game that the game component should be attached to.</param>
        public Joystick(Game game)
            : base(game)
        {

        }

        public override void Initialize()
        {
            base.Initialize();
            var inputManager = Game.GetService<InputManager>();
            inputManager.RegisterActionToDevice(PhoneDevices.PhoneGestureDevice, GestureType.FreeDrag, o => RaiseDragEvent(new GestureEventArgs((GestureSample)o)));
            inputManager.RegisterActionToDevice(PhoneDevices.PhoneGestureDevice, GestureType.DragComplete, o => RaiseEndDragEvent(new GestureEventArgs((GestureSample)o)));
        }

        /// <summary>
        /// Registers the default behavior.
        /// </summary>
        public void RegisterDefaultBehavior()
        {
            RegisterCommand(e =>
            {
                var data = (e as GestureEventArgs).Data;
                if (!Border.Contains(data.Position))
                    return;
                var camera = Game.GetService<GraphicManager>().CurrentCamera;
                FreeCamera freeCamera;
                if (camera is FreeCamera)
                {
                    freeCamera = camera as FreeCamera;
                    Vector3 translation = Vector3.Zero;
                    if (data.Delta.X != 0)
                    {
                        if (data.Delta.X > 0)
                            translation += (Vector3.Right);
                        else
                        {
                            translation += Vector3.Left;
                        }
                    }
                    if (data.Delta.Y != 0)
                    {
                        if (data.Delta.Y > 0)
                            translation += Vector3.Backward;

                        else
                        {
                            translation += Vector3.Forward;
                        }
                    }
                    freeCamera.Move(translation);
                }
            });
            Drag += (s, e) => SetActiveState(e);
            DragEnd += (s, e) => DeactiveState();
            var inputManager = Game.GetService<InputManager>();
            inputManager.RegisterActionToDevice(
                PhoneDevices.PhoneGestureDevice, GestureType.FreeDrag, o =>
                {

                    var data = (GestureSample)o;
                    if (Border.Contains(data.Position))
                        return;
                    FreeCamera camera = Game.GetService<GraphicManager>().CurrentCamera as FreeCamera;
                    if (camera != null)
                        camera.Rotate(data.Delta.X * 0.001f, data.Delta.Y * 0.001f);
                });
        }

    }
}